﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Common;

namespace Waiter.Controler
{
    class MenuCtrl
    {
        public List<MENU> fsGetMenuList(int status)
        {
            List<MENU> menuList = null;
            try
            {
                menuList = (from s in CommonGlobalVariable.globalDataContext.MENUs
                            where s.DEL_FLG == CommonConstants.FLAG_OFF
                                && s.CODE_STATUS == status
                            orderby s.DRINKS_NAME
                            select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsGetMenuList",
                    CommonUtils.fsParamListToString(new object[1] { status }),
                    ex.ToString());
            }
            return menuList;
        }

        public List<MENU> fsGetMenuList()
        {
            List<MENU> menuList = null;
            try
            {
                menuList = (from s in CommonGlobalVariable.globalDataContext.MENUs
                            where s.DEL_FLG == CommonConstants.FLAG_OFF
                            orderby s.DRINKS_NAME
                            select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsGetMenuList",
                    "",
                    ex.ToString());
            }
            return menuList;
        }

        public List<MENU> fsGetMenuListByGroup(string groupCode, int status)
        {
            List<MENU> menuList = null;
            try
            {
                menuList = (from s in CommonGlobalVariable.globalDataContext.MENUs
                            where s.DEL_FLG == CommonConstants.FLAG_OFF 
                                && s.CODE_GROUP == groupCode
                                && s.CODE_STATUS == status
                            orderby s.DRINKS_NAME
                            select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsGetMenuListByGroup",
                    CommonUtils.fsParamListToString(new object[2] { groupCode, status }),
                    ex.ToString());
            }
            return menuList;
        }

        public MENU fsGetMenuByCode(string menuCode)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.MENUs.First(q => q.CODE == menuCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsGetMenuListByGroup",
                    CommonUtils.fsParamListToString(new object[1] { menuCode }),
                    ex.ToString());
                return null;
            }
            
        }

        public long fsGetSalesPrice(string menuCode, string areaCode)
        {
            long price = 0;
            try
            {
                MasterCtrl masterCtrl = new MasterCtrl();
                MASTER master = masterCtrl.fsGetMasterFromGroupCode(CommonConstants.GIA_KHU_VUC, areaCode);
                if (master != null)
                {
                    price += Convert.ToInt64(master.VALUE);
                }

                MENU menu = fsGetMenuByCode(menuCode);
                if (menu != null)
                {
                    price += menu.UNIT_PRICE;
                }


            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsGetSalesPrice",
                    CommonUtils.fsParamListToString(new object[2] { menuCode, areaCode }),
                    ex.ToString());
            }
            return price;
        }

        public ResultStatus fsDeleteMenu(string menuCode)
        {
            try
            {
                MENU item = fsGetMenuByCode(menuCode);
                item.DEL_FLG = CommonConstants.FLAG_ON;
                item.OPID = CommonUtils.fsGetOPID();
                item.UPD_DATE = DateTime.Now;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsDeleteMenu",
                    CommonUtils.fsParamListToString(new object[1] { menuCode}),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public bool fsIsExistMenu(string menuName, string specialCode)
        {
            try
            {
                MENU item = CommonGlobalVariable.globalDataContext.MENUs.FirstOrDefault(q => q.CODE != specialCode
                   && q.DEL_FLG == CommonConstants.FLAG_OFF && q.DRINKS_NAME == menuName);
                return item != null;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsIsExistMenu",
                    CommonUtils.fsParamListToString(new object[2] { menuName, specialCode }),
                    ex.ToString());
                return CommonConstants.FLAG_OFF;
            }
        }

        public ResultStatus fsInsertMenu(MENU item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.MENUs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsInsertMenu",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsUpdateMenu(MENU item)
        {
            try
            {
                MENU updateItem = CommonGlobalVariable.globalDataContext.MENUs.FirstOrDefault(q=>q.CODE == item.CODE);
                updateItem.CODE_GROUP = item.CODE_GROUP;
                updateItem.CODE_STATUS = item.CODE_STATUS;
                updateItem.UNIT_CODE = item.UNIT_CODE;
                updateItem.UNIT_PRICE = item.UNIT_PRICE;
                updateItem.UPD_DATE = item.UPD_DATE;
                updateItem.OPID = item.OPID;
                updateItem.DRINKS_NAME = item.DRINKS_NAME;
                updateItem.DEL_FLG = item.DEL_FLG;

                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "MenuCtrl.fsUpdateMenu",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

    }
}
